aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/[username]/settings
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2022-11-02 19:47:29 -0700
committerAdam Soltys <[email protected]>2022-11-02 19:48:18 -0700
commit23ffc5a6359dbc84a1de1da632412edeb9989b5f (patch)
tree1cc276efeb5287e1fc176ec60d46aed091ab08ca /src/routes/(app)/[username]/settings
parentc12857a38eed1227ed51dc6e0af7242e66dd7561 (diff)
downloadcoinos-ui-23ffc5a6359dbc84a1de1da632412edeb9989b5f.tar.gz
coinos-ui-23ffc5a6359dbc84a1de1da632412edeb9989b5f.zip
move to app layout
Diffstat (limited to 'src/routes/(app)/[username]/settings')
-rw-r--r--src/routes/(app)/[username]/settings/+page.server.js21
-rw-r--r--src/routes/(app)/[username]/settings/+page.svelte104
-rw-r--r--src/routes/(app)/[username]/settings/_account.svelte255
-rw-r--r--src/routes/(app)/[username]/settings/_pos.svelte27
-rw-r--r--src/routes/(app)/[username]/settings/_security.svelte218
5 files changed, 625 insertions, 0 deletions
diff --git a/src/routes/(app)/[username]/settings/+page.server.js b/src/routes/(app)/[username]/settings/+page.server.js
new file mode 100644
index 00000000..ae7e0a78
--- /dev/null
+++ b/src/routes/(app)/[username]/settings/+page.server.js
@@ -0,0 +1,21 @@
+import { invalid } from '@sveltejs/kit';
+import { auth, get, post } from '$lib/utils';
+
+export const actions = {
+ default: async ({ cookies, request }) => {
+ let form = Object.fromEntries(await request.formData());
+
+ let user = {
+ ...(await get('/me', auth(cookies))),
+ ...form
+ };
+
+ try {
+ await post(`/user`, user, auth(cookies));
+ } catch (e) {
+ return invalid(400, { message: e.message });
+ }
+
+ return { success: true };
+ }
+};
diff --git a/src/routes/(app)/[username]/settings/+page.svelte b/src/routes/(app)/[username]/settings/+page.svelte
new file mode 100644
index 00000000..86705b1d
--- /dev/null
+++ b/src/routes/(app)/[username]/settings/+page.svelte
@@ -0,0 +1,104 @@
+<script>
+ import { onMount } from 'svelte';
+ import { fly } from 'svelte/transition';
+ import { enhance } from '$app/forms';
+ import Account from './_account.svelte';
+ import Pos from './_pos.svelte';
+ import Security from './_security.svelte';
+
+ import { Icon, Spinner, Pin } from '$comp';
+ import { t } from '$lib/translations';
+ import { failure, success } from '$lib/utils';
+ import { avatarUpload, bannerUpload, pin } from '$lib/store';
+ import { upload } from '$lib/upload';
+
+ export let data;
+ export let form;
+
+ let { user, rates, token } = data;
+ $: update(data);
+ let update = () => ({ user, rates, token } = data);
+
+ $: if (form?.message?.startsWith('Pin')) {
+ failure('Wrong pin, try again');
+ $pin = '';
+ }
+
+ let tab = 'account';
+
+ let tabs = [
+ { name: 'account', key: 'ACCOUNT', comp: Account },
+ { name: 'pos', key: 'POINT_OF_SALE', comp: Pos },
+ { name: 'security', key: 'SECURITY', comp: Security }
+ ];
+
+ $: ({ comp } = tabs.find((t) => t.name === tab));
+
+ let loading, submit;
+ let save = async (msg) => {
+ loading = true;
+ if (typeof msg !== 'string') msg = 'Settings saved';
+
+ try {
+ if ($avatarUpload) {
+ await upload($avatarUpload.file, $avatarUpload.type, $avatarUpload.progress, token);
+ }
+
+ if ($bannerUpload) {
+ await upload($bannerUpload.file, $bannerUpload.type, $bannerUpload.progress, token);
+ }
+
+ success(msg);
+ } catch (e) {
+ console.log(e);
+ failure('Something went wrong');
+ }
+
+ loading = false;
+ };
+
+ let loaded;
+ onMount(() => setTimeout(() => (loaded = true), 50));
+</script>
+
+{#if loaded && user.haspin && $pin?.length !== 6}
+ <Pin />
+{/if}
+
+<form method="POST" class="mb-[154px]" use:enhance on:submit={save}>
+ <input type="hidden" name="pin" value={$pin} />
+
+ <div class="mt-24 mb-20 px-3 md:px-0 w-full md:w-[400px] mx-auto space-y-8">
+ <h1 class="text-center text-3xl md:text-4xl font-semibold mb-10">
+ {$t('user.settings.header')}
+ </h1>
+
+ <div class="font-bold flex justify-between items-center border-b pb-3 text-secondary">
+ {#each tabs as { name, key }}
+ <button
+ class="hover:opacity-80"
+ class:text-black={tab === name}
+ on:click|preventDefault={() => (tab = name)}>{$t(`user.settings.${key}`)}</button
+ >
+ {/each}
+ </div>
+
+ <svelte:component this={comp} {user} {rates} {submit} />
+ </div>
+ <div
+ class="z-10 fixed bottom-0 bg-white shadow border-t w-full flex justify-center items-center py-3"
+ >
+ <button
+ bind:this={submit}
+ type="submit"
+ class="border-2 border-black rounded-xl font-semibold mx-auto py-3 w-40 hover:opacity-80"
+ class:bg-black={loading}
+ >
+ {#if loading}
+ <Spinner />
+ {:else}
+ <div class="my-auto">Save Settings</div>
+ {/if}
+ </button>
+ </div>
+</form>
diff --git a/src/routes/(app)/[username]/settings/_account.svelte b/src/routes/(app)/[username]/settings/_account.svelte
new file mode 100644
index 00000000..e49205ca
--- /dev/null
+++ b/src/routes/(app)/[username]/settings/_account.svelte
@@ -0,0 +1,255 @@
+<script>
+ import { tick } from 'svelte';
+ import { colorTheme, tempProfileFiles, avatarUpload, bannerUpload } from '$lib/store';
+ import { Icon, Toggle, LocaleSelector } from '$comp';
+ import { t } from '$lib/translations';
+ import { upload } from '$lib/upload';
+
+ export let user;
+
+ let selectedTheme = 1;
+ let revealPassword = false;
+ let password;
+ let profileFile;
+ let select = (type) => files[type].el.click();
+
+ $: profile = files.profile.src;
+ $: banner = files.banner.src;
+ $: $tempProfileFiles = { profile: files.profile.src, banner: files.banner.src };
+
+ let togglePassword = () => (revealPassword = !revealPassword);
+ let colorThemes = [
+ { theme: 1, color1: 'from-[#F5F7FA]', color2: 'to-[#C3CFE2]' },
+ { theme: 2, color1: 'from-[#FDFCFB]', color2: 'to-[#E2D1C3]' },
+ { theme: 3, color1: 'from-[#E6E9F0]', color2: 'to-[#EEF1F5]' },
+ { theme: 4, color1: 'from-[#D5D4D0]', color2: 'to-[#EEEEEC]' },
+ { theme: 5, color1: 'from-[#F3E7E9]', color2: 'to-[#E3EEFF]' },
+ { theme: 6, color1: 'from-[#F5F7FA]', color2: 'via-[#EAF2FF] to-[#DEDFFF]' }
+ ];
+
+ let handleThemeClick = (colors) => {
+ selectedTheme = colors.theme;
+ $colorTheme = colors.color1 + ' ' + colors.color2;
+ };
+
+ let percent;
+ let progress = async (event) => {
+ percent = Math.round((event.loaded / event.total) * 100);
+ };
+
+ let tooLarge = {};
+
+ let files = { profile: {}, banner: {} };
+ let handleFile = async ({ target }, type) => {
+ tooLarge[type] = false;
+ let file = target.files[0];
+ if (file.size > 10000000) return (tooLarge[type] = true);
+ var reader = new FileReader();
+ reader.onload = async (e) => {
+ files[type].src = e.target.result;
+ };
+ reader.readAsDataURL(file);
+
+ files[type].filename = file.name;
+ if (type === 'profile') {
+ $avatarUpload = { file, type, progress };
+ } else if (type === 'banner') {
+ $bannerUpload = { file, type, progress };
+ }
+ };
+
+ $: user.password = user.confirm = password;
+</script>
+
+<div>
+ <label for="language" class="font-bold block mb-1">{$t('user.settings.locale')}</label>
+ <LocaleSelector style="select-styles block py-3 w-full" />
+</div>
+
+<!-- TODO
+<div>
+ <label for="email" class="font-bold mb-1 block">{$t('user.settings.email')}</label>
+ <input type="email" name="email" bind:value={user.email} />
+</div> -->
+
+<!-- TODO fix breaking changes when user updates this field -->
+<div>
+ <label for="username" class="font-bold mb-1 block">{$t('user.settings.username')}</label>
+ <input disabled type="text" name="username" bind:value={user.username} />
+ <input type="hidden" name="username" bind:value={user.username} />
+</div>
+
+<div class="relative">
+ <label for="password" class="block font-bold block mb-1">{$t('user.settings.newPassword')}</label>
+ {#if revealPassword}
+ <input name="password" type="text" bind:value={password} />
+ {:else}
+ <input name="password" type="password" bind:value={password} />
+ {/if}
+ <button
+ type="button"
+ on:click={togglePassword}
+ on:keydown={togglePassword}
+ class="absolute right-5 top-11"
+ >
+ <Icon icon={revealPassword ? 'eye' : 'eye-off'} />
+ </button>
+</div>
+
+<div>
+ <label for="address" class="font-bold mb-1 block">{$t('user.settings.businessAddress')}</label>
+ <input type="text" name="address" bind:value={user.address} />
+</div>
+
+<!-- TODO -->
+<!-- <div> -->
+<!-- <div class="flex justify-between items-center"> -->
+<!-- <span class="font-bold">{$t('user.settings.emailNotifications')}</span> -->
+<!-- <Toggle id="email-notify" /> -->
+<!-- </div> -->
+<!-- <p class="text-secondary mt-1 w-9/12"> -->
+<!-- {$t('user.settings.emailNotificationsDescription')} -->
+<!-- </p> -->
+<!-- </div> -->
+<!-- -->
+<!-- <div> -->
+<!-- <div class="flex justify-between items-center"> -->
+<!-- <span class="font-bold">{$t('user.settings.smsNotifications')}</span> -->
+<!-- <Toggle id="sms-notify" /> -->
+<!-- </div> -->
+<!-- <p class="text-secondary mt-1 w-9/12"> -->
+<!-- {$t('user.settings.smsNotificationsDescription')} -->
+<!-- </p> -->
+<!-- </div> -->
+
+<div>
+ <span class="font-bold">{$t('user.settings.profileImage')}</span>
+
+ <div class="flex">
+ {#if profile}
+ <div
+ class="relative rounded-full overflow-hidden text-center w-20 h-20 my-auto hover:opacity-80 cursor-pointer"
+ on:click={() => select('profile')}
+ on:keydown={() => select('profile')}
+ >
+ <img
+ src={profile}
+ class="absolute w-full h-full object-cover object-center visible overflow-hidden"
+ alt={user.username}
+ />
+ </div>
+ {:else if user.profile}
+ <div
+ class="relative rounded-full overflow-hidden text-center w-20 h-20 my-auto hover:opacity-80 cursor-pointer"
+ on:click={() => select('profile')}
+ on:keydown={() => select('profile')}
+ >
+ <img
+ src={`/api/public/${user.username}-profile.webp`}
+ class="absolute w-full h-full object-cover object-center visible overflow-hidden"
+ alt={user.username}
+ />
+ </div>
+ {:else}
+ <div
+ class="rounded-full border-4 border-white p-4 bg-gradient-to-r {$colorTheme} w-24 my-auto hover:opacity-80 cursor-pointer"
+ on:click={() => select('profile')}
+ on:keydown={() => select('profile')}
+ >
+ <Icon icon="logo-symbol-white" style="mx-auto" />
+ </div>
+ {/if}
+ <div class="ml-2 p-2">
+ <!-- found missing translation -->
+ <button
+ type="button"
+ class="border rounded-2xl font-bold w-24 text-center px-0 py-2 hover:opacity-80"
+ on:click={() => select('profile')}
+ on:keydown={() => select('profile')}>Select</button
+ >
+ <input
+ type="file"
+ class="hidden"
+ bind:this={files.profile.el}
+ on:change={(e) => handleFile(e, 'profile')}
+ />
+ {#if files.profile.filename}
+ <div class="mt-2 text-sm">{files.profile.filename}</div>
+ {/if}
+ </div>
+ </div>
+
+ {#if tooLarge['profile']}
+ <div class="text-red-600">Max file size 10MB</div>
+ {/if}
+</div>
+
+<div>
+ <div class="flex justify-between items-center">
+ <span class="font-bold">{$t('user.settings.bannerImage')}</span>
+ </div>
+ <!-- found missing translation -->
+
+ {#if banner}
+ <img
+ src={banner}
+ class="w-full object-cover object-center visible overflow-hidden h-48 mb-4 hover:opacity-80"
+ on:click={() => select('banner')}
+ on:keydown={() => select('banner')}
+ alt="Banner"
+ />
+ {:else if user.banner}
+ <img
+ src={`/api/public/${user.username}-banner.webp`}
+ class="w-full object-cover object-center visible overflow-hidden h-48 mb-4 hover:opacity-80"
+ on:click={() => select('banner')}
+ on:keydown={() => select('banner')}
+ alt="Banner"
+ />
+ {:else}
+ <div
+ class="bg-gradient-to-r {$colorTheme} w-full h-48 mb-4 cursor-pointer hover:opacity-80"
+ on:click={() => select('banner')}
+ on:keydown={() => select('banner')}
+ alt="Banner"
+ />
+ {/if}
+
+ <!-- found missing translation -->
+ <button
+ type="button"
+ class="border rounded-2xl font-bold w-24 text-center px-0 py-2 hover:opacity-80"
+ on:click={() => select('banner')}
+ on:keydown={() => select('banner')}>Select</button
+ >
+ <input
+ type="file"
+ class="hidden"
+ bind:this={files.banner.el}
+ on:change={(e) => handleFile(e, 'banner')}
+ />
+ {#if files.banner.filename}
+ <div class="mt-2 text-sm">{files.banner.filename}</div>
+ {/if}
+
+ {#if tooLarge['banner']}
+ <div class="text-red-600">Max file size 10MB</div>
+ {/if}
+</div>
+
+<!-- TODO
+<div>
+ <span class="font-bold mb-1 block">{$t('user.settings.theme')}</span>
+ <p class="text-secondary mb-1">{$t('user.settings.themeDescription')}</p>
+ {#each colorThemes as colors}
+ <button
+ type="button"
+ class="hover:opacity-80 mr-2 w-10 h-10 bg-gradient-to-r {colors.color1} {colors.color2} rounded-xl border-2 {selectedTheme ===
+ colors.theme
+ ? 'border-black'
+ : ''}"
+ on:click={() => handleThemeClick(colors)}
+ on:keydown={() => handleThemeClick(colors)}
+ />
+ {/each}
+</div> -->
diff --git a/src/routes/(app)/[username]/settings/_pos.svelte b/src/routes/(app)/[username]/settings/_pos.svelte
new file mode 100644
index 00000000..cf4a5ece
--- /dev/null
+++ b/src/routes/(app)/[username]/settings/_pos.svelte
@@ -0,0 +1,27 @@
+<script>
+ import { Icon, Toggle } from '$comp';
+ import { t } from '$lib/translations';
+ import { success, failure } from '$lib/utils';
+
+ export let rates, user;
+
+ const fiats = Object.keys(rates);
+</script>
+
+<div>
+ <label for="currency" class="font-bold block mb-1">{$t('user.settings.localCurrency')}</label>
+ <select name="currency" class="select-styles block py-3 w-full">
+ {#each fiats as fiat}
+ <option value={fiat} selected={user.currency === fiat}>{fiat}</option>
+ {/each}
+ </select>
+</div>
+
+<!-- TODO
+<div>
+ <label for="unit" class="font-bold block mb-1">{$t('user.settings.btcUnit')}</label>
+ <select disabled name="unit" class="select-styles block py-3 w-full" bind:value={user.unit}>
+ <option value="SAT">{$t('user.settings.satoshis')} (SAT)</option>
+ <option value="BTC">{$t('user.settings.bitcoin')} (BTC)</option>
+ </select>
+</div> -->
diff --git a/src/routes/(app)/[username]/settings/_security.svelte b/src/routes/(app)/[username]/settings/_security.svelte
new file mode 100644
index 00000000..98b9b503
--- /dev/null
+++ b/src/routes/(app)/[username]/settings/_security.svelte
@@ -0,0 +1,218 @@
+<script>
+ import { tick } from 'svelte';
+ import { t } from '$lib/translations';
+ import { Icon, Qr } from '$comp';
+ import { Pincode, PincodeInput } from 'svelte-pincode';
+ import { post, success, failure } from '$lib/utils';
+ import { pin as current } from '$lib/store';
+
+ export let user, submit;
+
+ let settingPin, setting2fa, disabling2fa;
+
+ let token = '';
+ let pinCode = [];
+ let verifyCode = [];
+ let tokenInput, pinInput, verifyInput;
+ let verifying = false;
+ let old;
+
+ let startVerifying = () => {
+ verifying = true;
+ };
+
+ $: pin = pinCode.join('');
+
+ let locked;
+ $: verifyCode.join('').length > 5 && checkPin();
+ let checkPin = async () => {
+ old = $current;
+ if (locked) return;
+ locked = true;
+
+ let verify = verifyCode.join('');
+
+ pinCode = ['', '', '', '', '', ''];
+ verifyCode = ['', '', '', '', '', ''];
+ settingPin = false;
+
+ try {
+ if (pin.length > 5 && pin === verify) {
+ user.haspin = true;
+ $current = pin;
+ submit.click();
+ } else {
+ failure('Pin mismatch, try again');
+ settingPin = true;
+ pinInput.focusFirstInput();
+ }
+ } catch (e) {
+ failure('Problem setting PIN');
+ }
+
+ verifying = false;
+
+ await new Promise((r) => setTimeout(r, 500));
+ locked = false;
+ };
+
+ $: if (verifying) verifyInput && tick().then(verifyInput.focusFirstInput);
+ $: if (settingPin) pinInput && tick().then(pinInput.focusFirstInput);
+ $: if (setting2fa || disabling2fa) tokenInput && tick().then(tokenInput?.focusFirstInput);
+
+ let reset = () => {
+ token = '';
+ return true;
+ };
+
+ let togglePin = async () => {
+ if (user.haspin) {
+ try {
+ pin = null;
+ submit.click();
+ } catch (e) {
+ failure('Failed to disable pin');
+ }
+ } else {
+ settingPin = true;
+ }
+ };
+
+ let toggleEnabling = () => reset() && (setting2fa = !setting2fa);
+ let toggleDisabling = () => reset() && (disabling2fa = !disabling2fa);
+
+ $: enable2fa(token);
+ let enable2fa = async (twoFa) => {
+ try {
+ if (setting2fa && token.length === 6) {
+ await post('/enable2fa', { token });
+ success('2FA enabled');
+ user.twofa = 1;
+ toggleEnabling();
+ }
+ } catch (e) {
+ failure('Failed to enable 2FA, try again');
+ }
+ };
+
+ $: disable2fa(token);
+ let disable2fa = async () => {
+ try {
+ if (disabling2fa && token.length === 6) {
+ await post('/disable2fa', { token });
+ success('2FA disabled');
+ user.twofa = 0;
+ toggleDisabling();
+ }
+ } catch (e) {
+ failure('Failed to disable 2FA, try again');
+ }
+ };
+
+ $: otpUri = `otpauth://totp/coinos:${user.username}?secret=${user.otpsecret}&period=30&digits=6&algorithm=SHA1&issuer=coinos`;
+</script>
+
+<div>
+ <input type="hidden" name="newpin" value={pin} />
+
+ <span class="font-bold mb-1">{verifying ? 'Verify' : 'Security'} PIN</span>
+ <p class="text-secondary mb-1">
+ {$t('user.settings.securityPINDescription')}
+ </p>
+ {#if settingPin}
+ <div class="flex my-4">
+ <div class="mx-auto">
+ <div class:hidden={verifying}>
+ <Pincode on:complete={startVerifying} bind:code={pinCode} bind:this={pinInput}>
+ <PincodeInput />
+ <PincodeInput />
+ <PincodeInput />
+ <PincodeInput />
+ <PincodeInput />
+ <PincodeInput />
+ </Pincode>
+ </div>
+ <div class:hidden={!verifying}>
+ <Pincode bind:code={verifyCode} bind:this={verifyInput}>
+ <PincodeInput />
+ <PincodeInput />
+ <PincodeInput />
+ <PincodeInput />
+ <PincodeInput />
+ <PincodeInput />
+ </Pincode>
+ </div>
+ </div>
+ </div>
+ {:else}
+ <button type="button" class="primary" on:click={togglePin}
+ ><Icon icon="lock" style="mr-1" /> {user.haspin ? 'Disable Pin' : 'Enable Pin'}</button
+ >
+ {/if}
+</div>
+
+<!-- TODO
+<div>
+ <label for="auto-lock" class="font-bold mb-1">{$t('user.settings.autoLock')}</label>
+ <p class="text-secondary mb-1">
+ {$t('user.settings.autoLockDescription')}
+ </p>
+ <select name="auto-lock" class="select-styles block py-3 w-full">
+ <option value="5">5 min</option>
+ <option value="10">10 min</option>
+ <option value="30">30 min</option>
+ <option value="60">1 h</option>
+ <option value="480">8 h</option>
+ </select>
+</div> -->
+
+<div>
+ <span class="font-bold mb-1">{$t('user.settings.twofa')}</span>
+ <p class="text-secondary mb-4">
+ {$t('user.settings.twofaDescription')}
+ </p>
+ {#if setting2fa}
+ <a href={otpUri}>
+ <Qr text={otpUri} />
+ </a>
+
+ <div class="text-center my-4">
+ {$t('user.settings.accountId')}<br />
+ <b>{user.otpsecret}</b>
+ </div>
+
+ <div class="text-center my-4">
+ {$t('user.settings.oneTimeCode')}<br />
+ <Pincode bind:value={token} bind:this={tokenInput}>
+ <PincodeInput />
+ <PincodeInput />
+ <PincodeInput />
+ <PincodeInput />
+ <PincodeInput />
+ <PincodeInput />
+ </Pincode>
+ </div>
+ {:else if disabling2fa}
+ <div class="text-center my-4">
+ {$t('user.settings.oneTimeCode')}<br />
+ <Pincode bind:value={token} bind:this={tokenInput}>
+ <PincodeInput />
+ <PincodeInput />
+ <PincodeInput />
+ <PincodeInput />
+ <PincodeInput />
+ <PincodeInput />
+ </Pincode>
+ </div>
+ {:else if user.twofa}
+ <button type="button" class="primary" on:click={toggleDisabling}>
+ <Icon icon="mobile" style="mr-1" />
+ {$t('user.settings.twofaDisable')}
+ </button>
+ {:else}
+ <button type="button" class="primary" on:click={toggleEnabling}>
+ <Icon icon="mobile" style="mr-1" />
+ {$t('user.settings.twofaSetup')}
+ </button>
+ {/if}
+</div>